package com.wawayaya.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.wawayaya.bookreader.DefaultSettings;


/**
 * SharePreference  tool
 * @author  (last update author) David
 * @version 1.0
 * @created 2015-11-9
 */
public class PreferenceUtils {
    public  final static String ms_sp_wawayayareader = "wawayayareader";
    public  final static String MS_SP_CONFIG = "configUrl";
    private final static String ms_sp_dictmode="dictmode";
    private final static String SETTINGSLASTINNERPAGE = "lastinnerpage";

    public final static String ms_sp_wawayayareader_density = "density";
    public final static String ms_sp_wawayayareader_screenwidth = "ScreenWidth";
    public final static String ms_sp_wawayayareader_screenheight = "ScreenHeight";

    public final static String ms_sp_wawayayareader_horscreenwidth = "HorScreenWidth";
    public final static String ms_sp_wawayayareader_horscreenheight = "HorScreenHeight";

    public final static String ms_sp_wawayayareader_heapgrowthlimit = "heapgrowthlimit";

    private final static String KIDSIREADSETTINGS = "kidsireadsettings";
    private final static String SETTINGREADTOME = "readtome_mode";



    public static String getPrefString(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(key, Context.MODE_PRIVATE);
        return sp.getString(key,null);
    }

    public static String getPrefString(Context context, String key,
            final String defaultValue) {  
        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
        return settings.getString(key, defaultValue);
    }  
  
    public static void setPrefString(Context context, final String key, final String value) {
         SharedPreferences settings = context.getSharedPreferences(key,Context.MODE_PRIVATE);
        settings.edit().putString(key, value).commit();  
    }  
  
    public static boolean getPrefBoolean(Context context, final String key,  
            final boolean defaultValue) {  
        final SharedPreferences settings = PreferenceManager  
                .getDefaultSharedPreferences(context);  
        return settings.getBoolean(key, defaultValue);  
    }  
  
    public static boolean hasKey(Context context, final String key) {  
        return PreferenceManager.getDefaultSharedPreferences(context).contains(
                key);  
    }  
  
    public static void setPrefBoolean(Context context, final String key,  
            final boolean value) {  
        final SharedPreferences settings = PreferenceManager  
                .getDefaultSharedPreferences(context);  
        settings.edit().putBoolean(key, value).commit();  
    }  
  
    public static void setPrefInt(Context context, final String key,  
            final int value) {  
        final SharedPreferences settings = PreferenceManager  
                .getDefaultSharedPreferences(context);  
        settings.edit().putInt(key, value).commit();  
    }  
  
    public static int getPrefInt(Context context, final String key,  
            final int defaultValue) {  
        final SharedPreferences settings = PreferenceManager  
                .getDefaultSharedPreferences(context);  
        return settings.getInt(key, defaultValue);  
    }  
  
    public static void setPrefFloat(Context context, final String key,  
            final float value) {  
        final SharedPreferences settings = PreferenceManager  
                .getDefaultSharedPreferences(context);  
        settings.edit().putFloat(key, value).commit();  
    }  
  
    public static float getPrefFloat(Context context, final String key,  
            final float defaultValue) {  
        final SharedPreferences settings = PreferenceManager  
                .getDefaultSharedPreferences(context);  
        return settings.getFloat(key, defaultValue);  
    }  
  
    public static void setSettingLong(Context context, final String key,  
            final long value) {  
        final SharedPreferences settings = PreferenceManager  
                .getDefaultSharedPreferences(context);  
        settings.edit().putLong(key, value).commit();  
    }  
  
    public static long getPrefLong(Context context, final String key,  
            final long defaultValue) {  
        final SharedPreferences settings = PreferenceManager  
                .getDefaultSharedPreferences(context);  
        return settings.getLong(key, defaultValue);  
    }  
  
    public static void clearPreference(Context context,final SharedPreferences p) {
        final SharedPreferences.Editor editor = p.edit();
        editor.clear();
        editor.commit();
    }

    public static void remove(Context context, String key) {
        SharedPreferences sp = context.getSharedPreferences(key, 0);
        sp.edit().remove(key).commit();
    }

    public static String  getConfigUrlByKey(String  configName,Context ct) {
        SharedPreferences sp = ct.getSharedPreferences(MS_SP_CONFIG, Context.MODE_PRIVATE);
        return sp.getString(configName, "");
    }

    public static boolean getReadBookMode(Context ct, int bookid, String userid) {
        SharedPreferences sp = ct.getSharedPreferences(KIDSIREADSETTINGS + "_" + SETTINGREADTOME + "_" + String.valueOf(bookid), Context.MODE_PRIVATE);
        return sp.getBoolean(userid, false);
    }

    public static void saveReadBookMode(Context ct, int bookid, String userid, boolean readToMe) {
        SharedPreferences settings = ct.getSharedPreferences(KIDSIREADSETTINGS + "_" + SETTINGREADTOME + "_" + String.valueOf(bookid), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(userid, readToMe);
        editor.commit();
    }

    public static int readdeviceheapgrowthlimit(Context ct){
        SharedPreferences sp = ct.getSharedPreferences(ms_sp_wawayayareader, Context.MODE_PRIVATE);
        return sp.getInt(ms_sp_wawayayareader_heapgrowthlimit, 48);
    }

    /**
     * 设置 分辨率
     * @param ct
     * @return
     */
    public static Devicescreenbasicinfo readdevicescreenbasicinfo(Context ct) {
        SharedPreferences sp = ct.getSharedPreferences(ms_sp_wawayayareader, Context.MODE_PRIVATE);
        Devicescreenbasicinfo dsi = new Devicescreenbasicinfo();
        dsi.m_fscreendensity = sp.getFloat(ms_sp_wawayayareader_density, 1.0f);
        dsi.m_iScreenWidth = sp.getInt(ms_sp_wawayayareader_screenwidth, 0);
        dsi.m_iScreenHeight = sp.getInt(ms_sp_wawayayareader_screenheight, 0);
        return dsi;
    }

    public static Devicescreenbasicinfo readdevicescreenbasicinfo(Context ct, boolean bislandscape){
        if(bislandscape) {
            SharedPreferences sp = ct.getSharedPreferences(ms_sp_wawayayareader, Context.MODE_PRIVATE);
            Devicescreenbasicinfo dsi = new Devicescreenbasicinfo();
            dsi.m_fscreendensity = sp.getFloat(ms_sp_wawayayareader_density, 1.0f);
            dsi.m_iScreenWidth = sp.getInt(ms_sp_wawayayareader_horscreenwidth, 0);
            dsi.m_iScreenHeight = sp.getInt(ms_sp_wawayayareader_horscreenheight, 0);
            return dsi;
        }
        return readdevicescreenbasicinfo(ct);
    }

    public static int getLastReadInnerPage(Context ct, int bookid, String userid) {
        SharedPreferences sp = ct.getSharedPreferences(KIDSIREADSETTINGS + "_" + SETTINGSLASTINNERPAGE + "_" + String.valueOf(bookid) , Context.MODE_PRIVATE);

        return sp.getInt(userid, 0);
    }

    public static void saveLastReadInnerPage(Context ct, int bookid, String userid, int page) {
        SharedPreferences settings = ct.getSharedPreferences(KIDSIREADSETTINGS + "_" + SETTINGSLASTINNERPAGE + "_" + String.valueOf(bookid), Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(userid, page);
        editor.commit();
    }

    /**
     * 词典 mode
     * @param ct
     * @param defaultmode
     * @return
     */
    public static int getCurDictMode(Context ct, int defaultmode) {
        SharedPreferences sp = ct.getSharedPreferences(ms_sp_wawayayareader, Context.MODE_PRIVATE);
        return sp.getInt(ms_sp_dictmode, defaultmode);
    }

    /**
     * 设置 词典 mode
     * @param ct
     * @param dictmode
     */
    public static void setCurDictMode(Context ct, int dictmode) {
        SharedPreferences sp = ct.getSharedPreferences(ms_sp_wawayayareader, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putInt(ms_sp_dictmode, dictmode);
        editor.commit();
    }

    /**
     * put boolean preferences
     *
     * @param context
     * @param key     The name of the preference to modify
     * @param value   The new value for the preference
     * @return True if the new values were successfully written to persistent storage.
     */
    public static boolean putBoolean(Context context, String key, boolean value) {
        SharedPreferences settings = context.getSharedPreferences(ms_sp_wawayayareader, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    /**
     * get boolean preferences, default is false
     *
     * @param context
     * @param key     The name of the preference to retrieve
     * @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this
     * name that is not a boolean
     * @see #getBoolean(android.content.Context, String, boolean)
     */
    public static boolean getBoolean(Context context, String key) {
        return getBoolean(context, key, false);
    }

    /**
     * get boolean preferences
     *
     * @param context
     * @param key          The name of the preference to retrieve
     * @param defaultValue Value to return if this preference does not exist
     * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
     * this name that is not a boolean
     */
    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences settings = context.getSharedPreferences(ms_sp_wawayayareader, Context.MODE_PRIVATE);
        return settings.getBoolean(key, defaultValue);
    }
}  